<template>
  <div>
    <div style="display: flex; justify-content: center">
      <el-input></el-input>
      <el-button></el-button>
    </div>
    <div>
      <el-card v-for="(admin, index) in admin" :key="index">
        <div slot="header">
          <span>{{ admin.name }}</span>
        </div>
      </el-card>
    </div>
    <div>
      <el-popover
        placement="bottom"
        title="标题"
        width="200"
        trigger="click"
        @show="showPop(admin)"
        @hide="hidePop(admin)"
      >
        <el-select v-model="selecRoles" multiple placeholder="请选择">
          <el-option
            v-for="(r, index) in allRoles"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button slot="reference">click 激活</el-button>
      </el-popover>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchValue: {
        nationId: null,
        positionId: "",
      },
      admin: [],
      allRoles: [],
      selectRoles: [],
    };
  },
  methods: {
    hidePop(admin) {
      let roles = [];
      Object.assign(roles, admin.roles);
      let flag = false;
      if (roles.length != this.selectRoles.length) {
        flag = true;
      } else {
        for (let i = 0; i < roles.length; i++) {
          let role = roles[i];
          for (let j = 0; j < this.selectRoles.length; j++) {
            let sr = this.selectRoles[j];
            if (role.id == sr) {
              roles.splice(i, 1);
              i--;
              break;
            }
          }
        }
      }

      if (flag) {
        let url = "url/role?adminId=" + admin.id;
        this.selectRoles.forEach((sr) => {
          url += "&rids=" + sr;
        });
        this.putRequest(url).then((resp) => {
          if (resp) {
            this.initAdmins();
          }
        });
      }
    },
    showPop(admin) {
      this.initAllRoles();
      let roles = admin.roles;
      this.selectRoles = [];
      roles.forEach((r) => {
        this.selectRoles.push(r.id);
      });
    },
    initAdmins() {
      this.getRequest("url/?keywords=" + this.keywords).then((resp) => {
        if (resp) {
          this.admin = resp;
        }
      });
    },
    enabledChange(admin) {
      this.putRequest("url", admin).then((resp) => {
        if (resp) {
          this.initAdmins();
        }
      });
    },
    dosSearch() {
      this.initAdmins();
    },
    deleteAdmin(admin) {
      this.$confirm(
        "此操作将永久删除[" + this.admin.name + "], 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequest("url/" + admin.id).then((resp) => {
            if (resp) {
              this.initAdmins();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    initEmps(type) {
      this.loading = true;
      let url = "url/currentPage" + this.currentPage + "&size=" + this.size;
      if (type && type == "advanced") {
        if (this.searchValue.politicId) {
          url += "&politicId=" + this.searchValue.politicId;
        }
        if (this.searchValue.nationId) {
          url += "&nationId=" + this.searchValue.nationId;
        }
        if(this.searchValue.departId){
          url +="&departId" + this.searchValue.departId
        }
      }
      else{
        url += '&name=' + this.empName 
      }
      this.getRequest(url).then(resp =>{
        this.loading = false
        if(resp){
          this.emps = resp.data
          this.total = resp.total
        }
      })
    },
  },
  mounted() {
    this.initAdmins();
  },
};
</script>

<style>
</style>